package com.atxiaodei.kabudashejimoshi.责任链模式.使用建造者优化责任链模式;

import com.atxiaodei.kabudashejimoshi.责任链模式.Member;

/**
 *
 * 使用建造者优化责任链
 */
public abstract class Handler<T> {

    protected Handler chain;

    public void next(Handler handler){
        this.chain = handler;
    }

    public abstract void doHandle(Member member);

    public static class Builder<T> {
        private Handler<T> head;
        private Handler<T> tail;

        public Builder<T> addHandler(Handler<T> handler) {

            if (this.head == null) {
                this.head = this.tail = handler;
                return this;
            }
            this.tail.next(handler);
            this.tail = handler;

            return this;
        }

        public Handler<T> build() {
            return this.head;
        }
    }

}